package oldVersion.storage;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.media.Format;
import javax.media.MediaLocator;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import storage.RTPTransmit;


public class RTPServer extends Thread{
	private JTextArea msg;
	String hifiAddress ="";

	public RTPServer(JTextArea msg){
		this.msg=msg;
	}

	public RTPServer(){
		this.msg = null;
		
	}
	
	public void SetHifiAddress(String myHifiAddress){
		this.hifiAddress = myHifiAddress;
	}

	public void run() {

		String path;

		try		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception e)		{
			e.printStackTrace();
		}

		try {
//			path = JOptionPane.showInputDialog("Please Enter the directory path to share as [f:/audio/songs/]");
			path = "./src/mp3s/";
			System.out.println(path);
			File fi = new File(path);
			String sf[]= fi.list();
			for(int i=0; i<sf.length; i++){
				if (this.msg!=null)	msg.append(sf[i]+"\n");
				System.out.println(sf[i]);
			}

		
			String dip = hifiAddress;
			String rtpport = "5000";
			int p = 5000;
			ServerSocket ss = new ServerSocket(p);
			System.out.println("RTP Server Started, waiting for connections...");
			if (this.msg!=null) msg.append("RTP Server Started, waiting for connections...\n");
			while (true){
				Socket s = ss.accept();
				System.out.println("Connection request recieved");
				if (this.msg!=null) msg.append("Connection request recieved\n");

				BufferedReader FromClient = new BufferedReader(new InputStreamReader(s.getInputStream())); 
				String selectedFile = FromClient.readLine();

				OutputStream os = s.getOutputStream();
				PrintWriter pw = new PrintWriter(os,true);
				if(selectedFile.equals("os")){
					String length = Integer.toString(sf.length);
					pw.println(length);		
					for(int i=0; i<sf.length; i++){
						pw.println(sf[i]);
					}
				}
				else{
					System.out.println("RTPClient has selected " +selectedFile+ " to Play");
					if (this.msg!=null) msg.append("RTPClient has selected " +selectedFile+ " to Play+\n");
					Format fmt = null;
					String url = "file:"+path+selectedFile;
					System.out.println("looking for song at "+ url);
					if (this.msg!=null) msg.append("looking for song at "+ url+"\n");
					
					// where url is the local file path which will be transmitted
					RTPTransmit at = new RTPTransmit(new MediaLocator(url), dip , rtpport,fmt);
					
					double result = at.start();
					
//					if (result != null) {
//						System.err.println("Error : " + result);
//						if (this.msg!=null) msg.append("Error : "+result+"\n");
//						System.exit(0);
//					} 

					//Transmission starts for 2 mints
					try {
						Thread.currentThread().sleep(60000);
					} catch (InterruptedException ie) {
						ie.printStackTrace();
					}

					at.stop();
					System.out.println("Transmission ended");
					if (this.msg!=null) msg.append("Transmission ended\n");
				}
				s.close();
			}
		}catch (Exception ex){
			ex.printStackTrace();
		}
	}
}


